﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace ServeNHL.Models
{
    [MetadataType(typeof(userContactMeta))]
    public partial class USER_CONTACT
    {

        /// <summary>
        /// Given a UserViewModel, saves or updates the information
        /// </summary>
        /// <param name="user">UserViewModel to save or add</param>
        /// <returns>userID</returns>
        public int SaveUserContact(UserViewModel user)
        {
            int userID = 0;

            if(user.user.USER_ID != 0)
            {
                UserContactUpdate(user.contact);
            }
            else
            {
                //error with this line, commented out so avoid popups while testing
               // UserContactInsert(user.user, user.contact);
            }
            return userID;
        }// End of public int SaveUserContact(UserViewModel user)

        /// <summary>
        /// Adds a given user object and user_contact object to the database
        /// </summary>
        /// <param name="user">User object associated with the contact information to get the USER_ID</param>
        /// <param name="contact">Contact object to save to the database</param>
        /// <returns>User ID</returns>
        public int UserContactInsert(USER_CONTACT contact)
        {
            int userID = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var curUserContact = new USER_CONTACT
                {
                    USER_ID = contact.USER_ID,
                    FIRST_NAME = contact.FIRST_NAME,
                    LAST_NAME = contact.LAST_NAME,
                    EMAIL = contact.EMAIL,
                    PHONE = contact.PHONE,
                    CARRIER = contact.CARRIER,
                    CREATED_DATE = Helpers.createDate.getCreateDate(),
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate(),
                };

                dbconn.USER_CONTACT.Add(curUserContact);
                dbconn.SaveChanges();

                userID = curUserContact.USER_ID;
            }
            return userID;
        } // End of public int UserContactInsert(USER user, USER_CONTACT contact)

        /// <summary>
        /// Updates a given user_contact object in the database
        /// </summary>
        /// <param name="user">User object associated with the contact information</param>
        /// <param name="contact">Contact object to save to the database.</param>
        /// <returns>userID</returns>
        public int UserContactUpdate(USER_CONTACT contact)
        {
            int userID = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var tempUser = new USER_CONTACT();

                if (!String.IsNullOrEmpty(contact.USER_ID.ToString()) || contact.USER_ID > 0)
                {
                    tempUser.USER_ID = contact.USER_ID;
                }
                if (!String.IsNullOrEmpty(contact.FIRST_NAME))
                {
                    tempUser.FIRST_NAME = contact.FIRST_NAME;
                }

                if (!String.IsNullOrEmpty(contact.LAST_NAME))
                {
                    tempUser.LAST_NAME = contact.LAST_NAME;
                }

                if (!String.IsNullOrEmpty(contact.EMAIL))
                {
                    tempUser.EMAIL = contact.EMAIL;
                }

                if (!String.IsNullOrEmpty(contact.PHONE.ToString()))
                {
                    tempUser.PHONE = contact.PHONE;
                }

                if (!String.IsNullOrEmpty(contact.CARRIER))
                {
                    tempUser.CARRIER = contact.CARRIER;
                }


                dbconn.USER_CONTACT.Add(tempUser);
                dbconn.SaveChanges();

                userID = tempUser.USER_CONTACT_ID;
            }
            return userID;
        } // End of public int UserContactUpdate(USER user, USER_CONTACT contact)/

    }
}